package com.castle.pdftools.utils;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文件上传工具
 *
 * @author Dawn
 */
@UtilityClass
@Slf4j
public class MinioUploaderUtils {

    final String bucket = "test";

    MinioClient minioClient = SpringContextUtil.getBean(MinioClient.class);


    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile) {
        try {
            boolean test = minioClient.bucketExists(bucket);
            if (!test) {
                minioClient.makeBucket(bucket);
            }
            minioClient.putObject(bucket, multipartFile.getOriginalFilename(),
                    multipartFile.getInputStream(), multipartFile.getSize(), null, null, "application/octet-stream");
            log.info("文件上传成功");
        } catch (Exception e) {
            log.error("文件上传失败");
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    @SneakyThrows
    public String fileLink(String filename){
        return minioClient.getObjectUrl(bucket,filename);
    }
}
